package com.itheima.app.makeFriendmanager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.QuestionService;
import com.itheima.service.UserInfoService;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.UserLocationService;
import com.itheima.service.mongo.VisitorService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.NearUserVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.RecommendUserVo;
import com.itheima.vo.VisitorVo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MakeFriendmanager {
    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private RecommendUserService recommendUserService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @DubboReference
    private VisitorService visitorService;
    @DubboReference
    private UserLocationService userLocationService;
    @DubboReference
    private QuestionService questionService;
//历史访客
    public  ResponseEntity findVisitors() {
        Long userId = UserHolder.get().getId();
        //获得redis中的赏赐访问时间
        String lastLoginTime = stringRedisTemplate.opsForValue().get(ConstantUtil.LAST_ACCESS_TIME + userId);
        List<Visitor> visitors =null;
        if (StrUtil.isNotEmpty(lastLoginTime)) {
            visitors = visitorService.findeAccessUser(userId, Long.parseLong(lastLoginTime));
        }else {
            visitors = visitorService.findeAccessUser(userId, Long.parseLong("0"));
        }

        ArrayList<VisitorVo> visitorVoArrayList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(visitors)) {
            for (Visitor visitor : visitors) {
                VisitorVo visitorVo = new VisitorVo();
                UserInfo userInfo = userInfoService.findUserInfoById(visitor.getVisitorUserId());
               //封装userinfo跟缘分值
                visitorVo.setUserInfo(userInfo);
                RecommendUser recommendUser = recommendUserService.findRecommendUserByCondition(userId, visitor.getVisitorUserId());
                if (recommendUser!=null) {
                    visitorVo.setFateValue(recommendUser.getScore().longValue());
                }else {
                    visitorVo.setFateValue(0l);
                }

                visitorVoArrayList.add(visitorVo);
            }
        }
           stringRedisTemplate.opsForValue().set(ConstantUtil.LAST_ACCESS_TIME+userId,System.currentTimeMillis()+"");

        return ResponseEntity.ok(visitorVoArrayList);
    }


    //今日佳人
    public ResponseEntity findTodayBest() {
        //获得线程池里的userid
        Long touserId = UserHolder.get().getId();
        RecommendUser recommendUser = recommendUserService.findMaxScoreRecommendUser(touserId);
        //封装recommend
        RecommendUserVo recommendUserVo = new RecommendUserVo();
        if (recommendUser!=null) {
            UserInfo userInfo = userInfoService.findUserInfoById(recommendUser.getUserId());
            recommendUserVo.setUserInfo(userInfo);
            recommendUserVo.setFateValue(recommendUser.getScore().longValue());
        }
        return ResponseEntity.ok(recommendUserVo);

    }
    //推荐朋友
    public ResponseEntity findRecommendUserList(Integer pageNum, Integer pageSize) {
        Long touserId = UserHolder.get().getId();
        //根据touserid查询推荐好友列表
        PageBeanVo pageBeanVo= recommendUserService.findRecommendUserList(touserId,pageNum,pageSize);
        //创建结果集合
        List<RecommendUserVo> recommendUserVoList = new ArrayList<>();
        List<RecommendUser> recommendUsers = (List<RecommendUser>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(recommendUsers)) {
            //遍历集合
            for (RecommendUser recommendUser : recommendUsers) {
                //通过uesrid获得userinfo
                UserInfo userInfo = userInfoService.findUserInfoById(recommendUser.getUserId());
                //封装recommendvo
                RecommendUserVo recommendUserVo = new RecommendUserVo();
                recommendUserVo.setUserInfo(userInfo);
                recommendUserVo.setFateValue(recommendUser.getScore().longValue());
                recommendUserVoList.add(recommendUserVo);
            }
        }
        //将recommendvoList封装进pagevo
        pageBeanVo.setItems(recommendUserVoList);
        return ResponseEntity.ok(pageBeanVo);
    }
//用户详情封面
    public ResponseEntity findRecommendDetailHead(Long jiarenId) {
        Long userId = UserHolder.get().getId();
        RecommendUser recommendUser = recommendUserService.findRecommendUserByCondition(userId, jiarenId);
        RecommendUserVo recommendUserVo = new RecommendUserVo();
        UserInfo userInfo = userInfoService.findUserInfoById(recommendUser.getUserId());
        recommendUserVo.setUserInfo(userInfo);
        recommendUserVo.setFateValue(recommendUser.getScore().longValue());
        return ResponseEntity.ok(recommendUserVo);

    }
    //上传地理位置
    public void uploadLocation(String longitude, String latitude, String addrStr) {
        Long userId = UserHolder.get().getId();
        userLocationService.saveUserLocation(Double.parseDouble(longitude),Double.parseDouble(latitude),addrStr,userId);
    }
  //搜附近
    public ResponseEntity searchNearUser(String gender, Long distance) {
        Long userId = UserHolder.get().getId();
        //查询出所有附近人的id
        List<Long> nearUseridlist = userLocationService.searchNearUser(userId, distance);
        ArrayList<NearUserVo> nearUserVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(nearUseridlist)) {
            for (Long nearuserId : nearUseridlist) {
                //排除自己
                if (userId==nearuserId) {
                    continue;
                }
                UserInfo userInfo = userInfoService.findUserInfoById(nearuserId);
                if (!userInfo.getGender().equals(gender)) {
                    continue;
                }
                NearUserVo nearUserVo = new NearUserVo();
                nearUserVo.setUserId(nearuserId);
                nearUserVo.setAvatar(userInfo.getAvatar());
                nearUserVo.setNickname(userInfo.getNickname());
                nearUserVos.add(nearUserVo);
            }
        }
        return ResponseEntity.ok(nearUserVos);

    }
    //查看陌生人问题
    public ResponseEntity findByQuestions(Long jiarenId) {
        //根据佳人id 查找佳人陌生人问题
        Question question = questionService.findByUserId(jiarenId);

        //判断陌生人问题是否为空
        if (question==null) {
            question = new Question();
            return ResponseEntity.ok(question.getStrangerQuestion());
        }

        return  ResponseEntity.ok(question.getStrangerQuestion());
    }
}
